<?php
/*
 * 友情链接类
 * @package    logical
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: d2a29c0d61a341ca7af0fbc720238104cbd540ee $
 */
defined('IN_IXDCW') or exit('Access Denied');
class FriendLink {
	private $_itemid;
	private $_db;
	private $_table;
	private $_fields;
	private $_errmsg = errmsg;
	
	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getErrmsg() {
		return $this->_errmsg;
	}

    function __construct() {
		global $db;
		$this->_table = $db->getPre().'link';
		$this->_db = &$db;
		$this->_fields = array('listorder', 'title', 'style', 'username', 'addtime', 'editor', 'edittime', 'status', 'linkurl');
    }
	
	public function pass($post) {
		global $L;
		if(!is_array($post)) return false;
		if(!$post['username']) return $this->_($L['link_pass_username']);
		if(!$post['title']) return $this->_($L['link_pass_title']);
		if(!$post['linkurl']) return $this->_($L['link_pass_linkurl']);
		return true;
	}
	
	public function set($post) {
		global $MOD, $DT_TIME, $_username, $_userid;
		if(!$this->_itemid) $post['addtime'] = $DT_TIME;
		$post['edittime'] = $DT_TIME;
		$post['editor'] = $_username;
		if(!defined('DT_ADMIN')) $post = Str::htmlSpecialChars($post);
		return $post;
	}

	public function getOne() {
        return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid='$this->_itemid'");
	}
	
	public function getList($condition = '1', $order = 'listorder DESC, itemid DESC') {
		global $pages, $page, $pagesize, $offset;
		$r = $this->_db->getOne("SELECT COUNT(*) AS num FROM {$this->_table} WHERE $condition");
		$pages = pages($r['num'], $page, $pagesize);
		$lists = array();
		$result = $this->_db->query("SELECT * FROM {$this->_table} WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while($r = $this->_db->fetchArray($result)) {
			$r['title'] = set_style($r['title'], $r['style']);
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$lists[] = $r;
		}
		return $lists;
	}	
	
	public function add($post) {
		global $DT, $MOD, $module;
		$post = $this->set($post);
		$sqlk = $sqlv = '';
		foreach($post as $k=>$v) {
			if(in_array($k, $this->_fields)) { $sqlk .= ','.$k; $sqlv .= ",'$v'"; }
		}
        $sqlk = substr($sqlk, 1);
        $sqlv = substr($sqlv, 1);
		$this->_db->query("INSERT INTO {$this->_table} ($sqlk) VALUES ($sqlv)");
		$this->_itemid = $this->_db->insertId();
		return $this->_itemid;
	}

	public function edit($post) {
		global $DT, $MOD, $module;
		$post = $this->set($post);
		$sql = '';
		foreach($post as $k=>$v) {
			if(in_array($k, $this->_fields)) $sql .= ",$k='$v'";
		}
        $sql = substr($sql, 1);
	    $this->_db->query("UPDATE {$this->_table} SET $sql WHERE itemid=$this->_itemid");
		return true;
	}

	public function delete($itemid, $all = true) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { 
				$this->delete($v, $all); 
			}
		} else {
			$this->_itemid = $itemid;
			$r = $this->getOne();
			if($all) {
				$this->_db->query("DELETE FROM {$this->_table} WHERE itemid=$itemid");
			}
		}
	}

	public function check($itemid, $status = 3) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { 
				$this->check($v, $status); 
			}
		} else {
			$this->_db->query("UPDATE {$this->_table} SET status=$status WHERE itemid=$itemid");
		}
	}

	public function _($e) {
		$this->_errmsg = $e;
		return false;
	}
}

